<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/12
 * Time: 下午4:59
 */

namespace app\admin\controller;

use think\Request;
class Permission extends Common
{
    public function index () {
        $search = input('get.search' , '' , 'htmlspecialchars');
        $page_search = $search;
        $permission = db('permission');
        if ($search){

            $search = '%'.$search.'%';


            $where = '';
            $where .= ' `name` LIKE "'.$search;

            $permission->where($where);

        }
        $permission_list = $permission->paginate(5 , false , ['query'=>['search'=>$page_search]]);

        $current_page = input('get.page' , 1 , 'intval');

        $page = $permission_list->render();
        $page = str_replace(
            ['class="pagination"' , 'class="disabled"' , 'class="active"'],
            ['class="am-pagination tpl-pagination"' , 'class="am-disabled"' , 'class="am-active"'],
            $page
        );
        $request = Request::instance();
        if ( $request->isAjax() )
        {
            return json(['permission_list' => $permission_list  , 'page' => $page , 'current_page'=>$current_page]);
        }
        $this->assign('page' , $page);
        $this->assign('current_page' , $current_page);
        $this->assign('permission_list' , $permission_list);
        return view();
    }

    public function create ()
    {
        $permission = db('permission')->select();
        $this->assign('permission',$permission);
        return view();
    }

    public function store ()
    {
        $data['ords'] = input('post.ord');
        $data['name'] = input('post.name');
        $data['personal_name'] = input('post.personal_name');
        $data['pid'] = input('post.pid' , '0');
        $data['type'] = input('post.type' , '1');

        db('permission')->insert($data);
        $this->success('权限添加成功！');

    }
}